package httpserver;

import java.io.*;


/**
 * @version 1.0
 * @author Casper, Jackie, Jakob, Dennis
 */
public class ConfigHandler{
    private String root;
    private int port;
    private int shutdownPort;
    private String defaultPage;
    private File file = new File("./src/httpserver/config/ServerConfig.cfg");
    private FileWriter fw;
    private BufferedWriter buffWri;

    /**
     * Constructor for ConfigHandler, tests if the config file exists<br>
     * then either creates a new one or reads the existing one.
     * @throws IOException
     */
    public ConfigHandler() throws IOException {
        if(!file.exists()){
            createConfig();
        }
        readConfig();
    }

    /**
     * This method auto generates the config file with its default values
     * @throws IOException
     */
    private void createConfig() throws IOException{
        Server.LOGGER.info("Creating default config file");
        file = new File("./src/httpserver/config/ServerConfig.cfg");
        fw = new FileWriter(file);
        buffWri  = new BufferedWriter(fw);        
        buffWri.write("ServerHTMLRootDirectory = ./src/httpserver/htdocs");
        buffWri.newLine();
        buffWri.write("ServerPort = 8080");
        buffWri.newLine();
        buffWri.write("ServerDefaultPage = index.html");
        buffWri.newLine();
        buffWri.write("ServerShutdownPort = 8081");
        buffWri.close();
    }

    /**
     * This method reads the config file, checks if it is valid and if not<br>
     * it calls the createConfig() to auto generate a new config file.
     * @throws IOException, FileNotFoundException
     */
    private void readConfig() throws FileNotFoundException, IOException{
        boolean isRead = false;
        FileReader fr = new FileReader(file);
        BufferedReader buffRead = new BufferedReader(fr);

        String rootLine = buffRead.readLine();
        String portLine = buffRead.readLine();
        String pageLine = buffRead.readLine();
        String shutdownLine = buffRead.readLine();
        
        if(rootLine == null || portLine == null || portLine.equalsIgnoreCase("") || rootLine.equalsIgnoreCase("") || pageLine.equalsIgnoreCase("") || pageLine == null || shutdownLine == null || shutdownLine.equalsIgnoreCase("")){
            createConfig();
        }else{
            String[] rootArr = rootLine.split(" = ");
            String[] portArr = portLine.split(" = ");
            String[] pageArr = pageLine.split(" = ");
            String[] shutdownArr = shutdownLine.split(" = ");
            if(rootArr[0].equalsIgnoreCase("ServerHTMLRootDirectory")){
               root = rootArr[1];
            }else{
                Server.LOGGER.info("Config file invalid, creating new!");
                createConfig();
                readConfig();
            } if(portArr[0].equalsIgnoreCase("ServerPort")){
                port = Integer.parseInt(portArr[1]);
            }else{
                Server.LOGGER.info("Config file invalid, creating new!");
                createConfig();
                readConfig();
            } if(pageArr[0].equalsIgnoreCase("ServerDefaultPage")){
                defaultPage = pageArr[1];               
            } else{
                Server.LOGGER.info("Config file invalid, creating new!");
                createConfig();
                readConfig();
            }if(shutdownArr[0].equalsIgnoreCase("ServerShutdownPort")){
                shutdownPort = Integer.parseInt(shutdownArr[1]);
                Server.LOGGER.info("Config file read, continuing server operation");
                isRead = true;
            }else{
                Server.LOGGER.info("Config file invalid, creating new!");
                createConfig();
                readConfig();
            }
        }
    }

    /**
     * 
     * @return returns the root specified in the cfg file
     */
    public String getRoot(){
        Server.LOGGER.info("Retrieving ROOT directory: " + root);
        return root;
    }

    /**
     *
     * @return returns the default start page specified in the cfg file
     */
    public String getDefaultPage(){
        Server.LOGGER.info("Retrieving default server page: " + defaultPage);
        return defaultPage;
    }

       /**
     *
     * @return Returns the server port in the .cfg file
     */
    public int getPort(){
        Server.LOGGER.info("Retrieving server port: " + port);
        return port;
    }
    /**
     *
     * @return default shutdown port
     */
    public int getShutdownPort(){
        Server.LOGGER.info("Retrieving shutdown port: " + shutdownPort);
        return shutdownPort;
    }
}